package cn.entity.master;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

public class Fighter extends JPanel implements masterImp {
    public int getLevel() {
        return level;
    }

    public void setLevel(int level) {
        this.level = level;
    }

    private int level;
    private int index;

    public void setX(int x) {
        this.x = x;
    }

    public void setY(int y) {
        this.y = y;
    }

    private int x;
    private int y;
    private int hitPower;
    public void setPower(int power) {
        this.power = power;
    }
    private int power;
    private int powerSpeed;
    private int speed;
    private BufferedImage[] images;
    private BufferedImage image;
    private int Hp;
    public static boolean UP=false;
    public static boolean DOWN=false;
    public static boolean LEFT=false;
    public static boolean RIGHT=false;
    public static int stop=0;
    public static int move=1;
    private int state;
    private int i;
    private int bigSword;

    public Fighter() throws IOException {
        level=1;
        powerSpeed=0;
        state=1;
        power=100;
        speed=9;
        x=50;
        y=200;
        index = 1;
        i=5;
        hitPower=0;
        bigSword=0;
         images = new BufferedImage[4];

         for(int i = 0; i < images.length; i++) {
             images[i]= ImageIO.read(new File("img/fighter/"+i+".png"));
         }
        image = images[0];
    }
    public void setSpeed(int speed) {
        this.speed = speed;
    }
    public void step(){
        if(UP && y>5&& state==1){
            y-=speed;
        }
       else if(DOWN&&y<500&& state==1){
            y+=speed;
        }
        else if(LEFT && x>-15&& state==1){
            x-=speed;
        }
        else if(RIGHT&&x<730&& state==1){
            x+=speed;
        }
    }
    @Override
    public int getX() {
        return x;
    }
    public void downX() {
        this.x-=speed;
    }
    @Override
    public int getY() {
        return y;
    }
    public void downY() {
        this.y-=speed;
    }
    public void upX() {
        this.x+=speed;
    }
    public void upY() {
        this.y+=speed;
    }
    public int getIndex(){
        return index;
    }
    public void setIndex(int index){
        this.index = index;
    }
    public int getImageLength(){
        return images.length;
    }
    public void setImage(int index){
        this.image = images[index];
    }
    public BufferedImage getImage(){
        return image;
    }
    public void setState(boolean a){
        if(a){
            state=move;
        }
        else state=stop;

    }
public void paintComponent(Graphics g){
    g.drawImage(getImage(),getX(),getY(),null);
    g.setColor(Color.BLUE);
    g.fillRect(0,30,power,7);
    String power1=Integer.toString(power);
    String lvl=Integer.toString(level);
    g.drawString(power1,power+5,40);
    g.drawString("等级"+lvl,0,50);
    g.setColor(Color.YELLOW);
    // 蓄力条显示 - 只在蓄力时显示
    if(bigSword > 0) {
        g.setColor(Color.YELLOW);
        g.fillRect(x+20, y-20, bigSword, 5); // 固定高度为5，更清晰
    }
//    Graphics2D g2d = (Graphics2D) g;
//    g2d.setColor(Color.white);
//    BasicStroke thickStroke = new BasicStroke(3.0f);
//    g2d.setStroke(thickStroke);
//    g2d.drawArc(200, 100, 300,180 , -90, -180);
//    g2d.drawArc(200, 150, 200,120 , -90, -180);

}
public void setHp(int hp){
        this.Hp=hp;
}
public int getHp(){
        return Hp;
}
    @Override
    public int getWidth() {
        return image.getWidth();
    }

    @Override
    public int getHitWidth() {
        return 0;
    }

    @Override
    public int getHeight() {
        return image.getHeight();
    }
    public void setBigSword(int key){
        if(key == 1 && bigSword < 35) {
            if(level == 1) {
                bigSword += level;
            } else if(level > 1) {
                bigSword += level - 1;
            }

            if(bigSword > 35) {
                bigSword = 35;
            }
        } else if(key == 0){
            bigSword = 0;
        }
    }

    public void chargeBigSword() {
        if(bigSword < 35) {
            if(level == 1) {
                bigSword += level;
            } else if(level > 1) {
                bigSword += level - 1;
            }
            if(bigSword > 35) {
                bigSword = 35;
            }
        }
    }

    public boolean canTriggerAttack() {
        return bigSword >= 35;
    }

    public void resetBigSword() {
        this.bigSword = 0;
    }

    public int getBigSwordValue() {
        return bigSword;
    }
   public int geti(){
        return i;
   }
   public boolean setPower(){
        if(level==1){
            if(power>=80){
                power-=80;
                return true;
            }
        }
        else if(level==2){
            if(power>=60){
                power-=60;
                return true;
            }

        }
        else if(level==3){
            if(power>=40){
                power-=40;
                return true;
            }

        }
        else if(level==4){
            if(power>=20){
                power-=20;
            }
        }

        return false;
   }
   public void upPower(){
        if(power<100){
            powerSpeed++;
            if(powerSpeed==6-2*level){
                power+=1;
                powerSpeed=0;
            }
        }
   }
   public void upHp(){
        if(power>60){
            this.Hp+=30;
        }
   }
   public void getPowerWhitSword(int i){
        if(power+i<=100){
            hitPower++;
            if(hitPower==6-2*level &&level<4){
                power+=i+(level-1);
                hitPower=0;
            }
            else if(level==4){
                power+=i+(level-1);
            }
        }
        if(power+i>100){
            power=100;
        }
    }
    public void getPower(int i){
        if(power+i<=100){
            power+=i*(level);
        }
        if(power+i>100){
            power=100;
        }
    }
}
